package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageDTOResponseEntity(
            @RequestParam(value = "page",required = false)Long page,
            @RequestParam(value ="rows",required = false)Long rows,
            @RequestParam(value ="brandId",required = false)Long brandId,
            @RequestParam(value ="id",required = false)Long id,
            @RequestParam(value ="saleable",required = false)Boolean saleable,
            @RequestParam(value ="categoryId",required = false)Long categoryId
    ){
        PageDTO<SpuDTO> spuDTOPageDTO = spuService.pageDTOResponseEntity(page, rows, brandId, id, saleable, categoryId);
        return ResponseEntity.ok(spuDTOPageDTO);

    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecGroupDTO>> getDTOResponseEntity(
            @RequestParam(value ="id")Long id,
            @RequestParam(value ="searching",required = false)Boolean searching
    ){
        List<SpecGroupDTO> dtoResponseEntity = spuService.getDTOResponseEntity(id, searching);
        return ResponseEntity.ok(dtoResponseEntity);
    }
    @PostMapping("spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
    @PutMapping("spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
       return ResponseEntity.ok(skuService.listPySpuId(id));
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id") Long id){
        return ResponseEntity.ok(detailService.querySpuDetail(id));
    }
}